package com.zatech.cgnci.project.service.impl;

import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zatech.cgnci.project.base.context.UserContext;
import com.zatech.cgnci.project.base.enums.ResponseCodeEnum;
import com.zatech.cgnci.project.base.enums.StatisticalTypeEnum;
import com.zatech.cgnci.project.base.exception.GlobalException;
import com.zatech.cgnci.project.base.utils.ConvertUtils;
import com.zatech.cgnci.project.dto.ReportCreateDTO;
import com.zatech.cgnci.project.dto.StatisticalReportDTO;
import com.zatech.cgnci.project.entity.*;
import com.zatech.cgnci.project.mapper.CgnciStatisticalReportMapper;
import com.zatech.cgnci.project.service.CgnciClaimsInventoryService;
import com.zatech.cgnci.project.service.CgnciManageReportService;
import com.zatech.cgnci.project.service.CgnciOutstandingClaimsAnnualSummaryService;
import com.zatech.cgnci.project.service.CgnciStatisticalReportService;
import com.zatech.cgnci.project.vo.StatisticalReportVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 统计报表 服务实现类
 * </p>
 *
 * @author Generator
 * @since 2025-02-19
 */
@Service
public class CgnciStatisticalReportServiceImpl extends ServiceImpl<CgnciStatisticalReportMapper, CgnciStatisticalReportDO> implements CgnciStatisticalReportService {
    @Resource
    private CgnciOutstandingClaimsAnnualSummaryService annualSummaryService;
    @Resource
    private CgnciManageReportService manageReportService;
    @Resource
    private CgnciClaimsInventoryService claimsInventoryService;

    @Override
    public IPage<StatisticalReportVO> pageData(StatisticalReportDTO queryDTO) {
        return this.baseMapper.pageData(queryDTO.toPage(), queryDTO);
    }

    @Override
    public StatisticalReportVO verify(StatisticalReportDTO queryDTO) {
        return this.baseMapper.queryData(queryDTO);
    }

    @Override
    @Transactional
    public void createReport(ReportCreateDTO extractDTO) {
        UserEntity user = UserContext.getUser();
        //先删除数据再插入
        if (extractDTO.getRegisterId() != null) {
            delStatisticalReport(extractDTO.getRegisterId(), extractDTO.getBusinessType());
        }
        CgnciStatisticalReportDO reportDO = ConvertUtils.convert(extractDTO, CgnciStatisticalReportDO.class);
        reportDO.setControlsDate(new Date());
        reportDO.setCreator(user.getNameAndCode());
        this.baseMapper.insert(reportDO);
        extractDTO.setRegisterId(reportDO.getRegisterId());
        switch (StatisticalTypeEnum.getEnumByCode(extractDTO.getBusinessType())) {
            case CLAIM_INVENTORY:
                claimsInventoryService.createClaimsInventory(extractDTO);
                break;
            case BUSINESS_OPERATIONS:
                manageReportService.saveManageReport(extractDTO);
                break;
            case OUTSTANDING_CLAIMS:
                annualSummaryService.annualSummary(extractDTO);
                break;
            case SUPERVISION:
                break;
            default:
                throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
    }

    /**
     * 文件下载
     *
     * @param registerId
     * @param businessTypeCode
     * @return
     */
    @Override
    public ExcelWriter export(Long registerId, String businessTypeCode) {
        switch (StatisticalTypeEnum.getEnumByCode(businessTypeCode)) {
            case CLAIM_INVENTORY:
                return claimsInventoryService.exportClaimsInventory(registerId);
            case BUSINESS_OPERATIONS:
                return manageReportService.exprotManageReport(registerId);
            case OUTSTANDING_CLAIMS:
                return annualSummaryService.outstandingClaimsAnnualSummary(registerId);
            case SUPERVISION:
                return null;
            default:
                throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }
    }

    private void delStatisticalReport(Long registerId, String businessType) {
        this.baseMapper.deleteById(registerId);
        switch (StatisticalTypeEnum.getEnumByCode(businessType)) {
            case CLAIM_INVENTORY:
                claimsInventoryService.remove(new LambdaQueryWrapper<CgnciClaimsInventoryDO>()
                        .eq(CgnciClaimsInventoryDO::getRegisterId, registerId));
                break;
            case BUSINESS_OPERATIONS:
                manageReportService.remove(new LambdaQueryWrapper<CgnciManageReportDO>()
                        .eq(CgnciManageReportDO::getRegisterId, registerId));
                break;
            case OUTSTANDING_CLAIMS:
                //删除年度汇总报表
                annualSummaryService.remove(new LambdaQueryWrapper<CgnciOutstandingClaimsAnnualSummaryDO>()
                        .eq(CgnciOutstandingClaimsAnnualSummaryDO::getRegisterId, registerId));
                break;
            case SUPERVISION:
                break;
            default:
                throw new GlobalException(ResponseCodeEnum.HTTP_REQUEST_SERVICE_ERROR);
        }

    }
}
